<?php
	
/* 
	
	dkpm - a DKP management system
	
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/

	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Copyright (C) 2007 Martin Wedvich. All Rights Reserved.
	
	-
	
	analyzelog.php
	This page deals with extracting and displaying data from uploaded combat logs.

*/
	
	define('dkpm', true);
	session_start();
	
	require 'functions.php';
	require 'framework.php';
	
	if($_GET['log'] < 1) {
		$_GET['log'] = 1;
	}
	
	$sql = mysql_query('SELECT log_raid, log_character, log_data FROM dkpm_combatlogs WHERE log_id=' . $_GET['log']);
	if($combatlog = mysql_fetch_row($sql)) {
	 	$recorder = mysql_result(mysql_query('SELECT character_name FROM dkpm_characters WHERE character_id=' . $combatlog[1]), 0, 0);
		$eventlist = explode(';', $combatlog[2]); 
	}
	
	if(!isset($_GET['show'])) {
		$_GET['show'] = 'summary';
	}

	switch($_GET['show']) {
	 	case('eventlist'): {
	 	 	$eventcount = count($eventlist);
	 		foreach($eventlist as $event) {
	 		 	$event = substr($event, strpos($event, ' ') + 1);
	 		 	$timesplit = strpos($event, ' ');
	 		 	$eventtime = substr($event, 0, $timesplit);
	 		 	$event = substr($event, $timesplit + 2);
				$rows .= '			
									<tr class="row">
										<td width="96">' . $eventtime . '</td>
										<td>' . $event . '</td>	
									</tr>';
			}
			$addtitle = ' &raquo; Event list';
			$content = '
					<p class="topborder">There are ' . $eventcount . ' events in this combat log.</p>
					<table width="100%" cellspacing="0" cellpadding="3">	
						<tr>
							<td width="96">Time</td>
							<td>Action</td>		
						</tr>	
					</table>
					<table bgcolor="#96BA32" width="100%" cellspacing="0" cellpadding="0">		
						<tr>
							<td width="100%">
								<table width="100%" cellspacing="1" cellpadding="2">' . $rows . '
								</table>
							</td>
						</tr>
					</table>';
			break;
		}
	 	case('summary'): {
	 	 	$addtitle = ' &raquo; Summary';
	 	 	$content = '';
			break;
		}
	 	case('timespan'): {
	 	 	// horrible, messy section below; I'll clean that up in not too long
	 	 	$firsteventdate = explode('/', substr($eventlist[0], 0, strpos($eventlist[0], ' ')));
	 		if(strlen($firsteventdate[0]) == 1) {
				$firsteventdate[0] = '0' . $firsteventdate[0];
			}
	 		if(strlen($firsteventdate[1]) == 1) {
				$firsteventdate[1] = '0' . $firsteventdate[1];
			}
	 		$firsteventtime = substr($eventlist[0], strpos($eventlist[0], ' ') + 1); 		
	 		$logstart = '2007' . $firsteventdate[0] . $firsteventdate[1] . str_replace(':', '', substr($firsteventtime, 0, strpos($firsteventtime, '.')));
	 		$lasteventdate = explode('/', substr($eventlist[count($eventlist) - 1], 0, strpos($eventlist[count($eventlist) - 1], ' ')));
	 		if(strlen($lasteventdate[0]) == 1) {
				$lasteventdate[0] = '0' . $lasteventdate[0];
			}
	 		if(strlen($lasteventdate[1]) == 1) {
				$lasteventdate[1] = '0' . $lasteventdate[1];
			}
	 		$lasteventtime = substr($eventlist[count($eventlist) - 1], strpos($eventlist[count($eventlist) - 1], ' ') + 1); 		
	 		$logend = '2007' . $lasteventdate[0] . $lasteventdate[1] . str_replace(':', '', substr($lasteventtime, 0, strpos($lasteventtime, '.')));
	 		$raidinfo = mysql_fetch_row(mysql_query('SELECT raid_start, raid_end FROM dkpm_raids WHERE raid_id=' . $combatlog[0]));
	 		$raidstart = str_replace(' ', '', str_replace('-', '', str_replace(':', '', $raidinfo[0])));
	 		$raidend = str_replace(' ', '', str_replace('-', '', str_replace(':', '', $raidinfo[1])));
	 		$raidtime = $raidend - $raidstart;
	 		$logtime = $logend - $logstart;
	 		$logrelativestart = $logstart - $raidstart;
	 		$addtitle = ' &raquo; Timespan';
	 	 	$content = '
			  		<p class="topborder">This combat log covers ' . round($logtime / $raidtime * 100, 2) . '% of the raid duration.</p>
			  		<table width="100%" cellspacing="0" cellpadding="0">
			  			<tr>
			  				<td width="' . ($logrelativestart / $raidtime * 200) . '%" align="center">' . $logstart . '</td>
			  				<td>&nbsp;</td>
			  			</tr>
			  		</table>
					<table bgcolor="#96BA32" width="100%" cellspacing="0" cellpadding="0">		
						<tr>
							<td width="100%">
								<table width="100%" cellspacing="1" cellpadding="0">
									<tr class="row">
										<td>
											<table width="100%" cellspacing="0" cellpadding="0">
												<tr>
													<td width="' . ($logrelativestart / $raidtime * 100) . '%">&nbsp;</td>
													<td class="partbar" width="' . ($logtime / $raidtime * 100) . '%">&nbsp;</td>
													<td>&nbsp;</td>
												</tr>
											</table>
										</td>
									</tr>
								</table>
							</td>
						</tr>
					</table>
					<table width="100%" cellspacing="0" cellpadding="0">
			  			<tr>
			  				<td width="' . ((($logrelativestart + $logtime)/ $raidtime * 100) - ((100 - ($logrelativestart + $logtime)/ $raidtime * 100)* 2)) . '">&nbsp;</td>
			  				<td>' . $logend . '</td>
			  			</tr>
			  		</table>';
			break;
		}
	}
	
	$title = 'dkpm &raquo; Analyze combat log #' . $_GET['log'] . $addtitle;
	$content = '<div id="submenu">
					<ul>
						<li><a class="tab" href="viewlog.php?log=' . $_GET['log'] . '&amp;show=summary" title="Summary">Summary</a></li> 
						<li><a class="tab" href="viewlog.php?log=' . $_GET['log'] . '&amp;show=eventlist" title="Event list">Event list</a></li> 
						<li><a class="tab" href="viewlog.php?log=' . $_GET['log'] . '&amp;show=timespan" title="Timespan">Timespan</a></li> 
					</ul>
				</div>
				<div id="subcontent">
					<p><span class="header1">Combat log #' . $_GET['log'] . '</span></p>
			  		<p><a class="tab" href="viewcharacter.php?character=' . $combatlog[1] . '" title="View ' . $recorder . '\'s character profile">' . $recorder . '</a> recorded this in <a class="tab" href="viewraid.php?raid=' . $combatlog[0] . '" title="View raid #' . $combatlog[0] . '">raid #' . $combatlog[0] . '</a>.</p>
					' . $content . '
				</div>';
		
	framework_exec($title, $content);

?>